package dao;

import com.alibaba.fastjson.JSON;
import domin.Book;
import domin.Bookloss;
import lombok.extern.slf4j.Slf4j;
import util.CommonDao;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.TreeSet;

@Slf4j
public class BooklossDao {
    private static BooklossDao booklossDao = new BooklossDao();
    private BooklossDao(){}
    public static BooklossDao getInstance(){
        return booklossDao;
    }
    private PreparedStatement preparedStatement;
    private ResultSet resultSet;
    public Bookloss find (Connection connection, int id) throws SQLException {
        Bookloss bookloss = null;
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM bookloss WHERE id=?");
        preparedStatement.setInt(1,id);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            bookloss= new Bookloss();
            bookloss.setId(resultSet.getInt("id"));
            bookloss.setReason(resultSet.getString("reason"));
            Book book = BookDao.getInstance().find(connection,resultSet.getInt("book_id"));
            bookloss.setBook(book);
        }
        JdbcHelper.close(resultSet,preparedStatement);
        return bookloss;
    }
    public Collection<Bookloss> findAll(Connection connection, Collection<Condition> condition, Pagination pagination) throws SQLException {
        Collection<Bookloss> booklosses = new HashSet<>();
        int totalNum = CommonDao.getInstance().count(connection,"bookloss");
        String sql_select_all = "SELECT * FROM bookloss";
        if (condition != null){
            sql_select_all += Condition.toWhereClause(JSON.toJSONString(condition));
            log.trace(sql_select_all);
        }
        if (pagination != null){
            sql_select_all += pagination.toLimitClause(totalNum);
            log.trace(sql_select_all);
        }
        this.preparedStatement = connection.prepareStatement(sql_select_all);
        this.resultSet = preparedStatement.executeQuery();
        System.out.println(1);
        while (this.resultSet.next()){
            Bookloss bookloss= new Bookloss();
            bookloss.setId(resultSet.getInt("id"));
            bookloss.setReason(resultSet.getString("reason"));
            Book book = BookDao.getInstance().find(connection,resultSet.getInt("book_id"));
            bookloss.setBook(book);
            booklosses.add(bookloss);

        }
        JdbcHelper.close(this.resultSet,this.preparedStatement);
        return booklosses;
    }

    public boolean add(Connection connection , Bookloss bookloss) throws SQLException {
        PreparedStatement preparedStatement =
                connection.prepareStatement(
                        "INSERT INTO " +
                                "bookloss (reason,book_id)" +
                                "VALUES(?,?)"
                );
        preparedStatement.setString(1,bookloss.getReason());
        preparedStatement.setInt(2,bookloss.getBook().getId());
        int rowAffected = preparedStatement.executeUpdate();
        JdbcHelper.close(null,preparedStatement);
        return rowAffected>0;
    }
    public boolean update(Connection connection,Bookloss bookloss) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement("update bookloss set reason=?,book_id=? where id=?");
        preparedStatement.setString(1,bookloss.getReason());
        preparedStatement.setInt(2,bookloss.getBook().getId());
        preparedStatement.setInt(3,bookloss.getId());
        int rowAffected = preparedStatement.executeUpdate();
        JdbcHelper.close(null,preparedStatement);
        return rowAffected > 0;
    }

    public boolean delete(Connection connection,Integer id) throws SQLException {
        this.preparedStatement = connection.prepareStatement("delete from bookloss where id=" + id);
        int rowAffected = preparedStatement.executeUpdate();
        JdbcHelper.close(null,preparedStatement);
        return rowAffected>0;
    }
}
